package com.zjdiepu.www.biz.other;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.ModeIDType;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.other.OtherSimpleInfoMapper;
import com.zjdiepu.www.ibiz.other.IOtherSimpleInfoService;
import com.zjdiepu.www.ibiz.zpp.SystemService;
import com.zjdiepu.www.model.other.dto.OtherSimpleInfo;
import com.zjdiepu.www.model.other.ext.OtherSimpleInfoExt;

/**
 * other_简单信息表,一些简单的记录型数据统一写在这,用modeid区分Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2018年01月09日 11时24分<br/>
 * @since 1.0<br/>
 */
@Service
public class OtherSimpleInfoServiceImpl extends BaseService implements IOtherSimpleInfoService {
	public static final Logger logger = LoggerFactory.getLogger(OtherSimpleInfoServiceImpl.class);

	@Autowired
	private SystemService systemService;
	@Autowired
	private OtherSimpleInfoMapper otherSimpleInfoMapper;
	
	@Override
	public void savePostSupplement(AbstractSysUser currentUser, OtherSimpleInfo otherSimpleInfo, boolean mobile) {
		if(StringUtils.isNotBlank(otherSimpleInfo.getId())) {
			otherSimpleInfo.initUpdateData(otherSimpleInfo.getModifier(), otherSimpleInfo.getModifierName());
			int updateCount = otherSimpleInfoMapper.updateByPrimaryKeySelective(otherSimpleInfo);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
		} else {
			otherSimpleInfo.initInsertData(otherSimpleInfo.getModifier(), otherSimpleInfo.getModifierName());
			otherSimpleInfoMapper.insert(otherSimpleInfo);
		}
		if(!mobile) {
			String receiverIds = otherSimpleInfo.getReceiverIds();
//	站内信	资审一审、电审一审、资审二审、电审二审
			systemService.diepuNewsHandle(currentUser, ModeIDType.HBZL.getCode(), "34", otherSimpleInfo.getBusiId(), null, receiverIds, otherSimpleInfo.getRemarks());
		}
	}
	
	@Override
	public void saveUpdateAndDelete(OtherSimpleInfo otherSimpleInfo, OperateType operType) {
		switch (operType) {
		case add:
			otherSimpleInfo.initInsertData(otherSimpleInfo.getModifier(), otherSimpleInfo.getModifierName());
			otherSimpleInfoMapper.insert(otherSimpleInfo);
			break;
		case edit:
			otherSimpleInfo.initUpdateData(otherSimpleInfo.getModifier(), otherSimpleInfo.getModifierName());
			// 影响的记录数
			int updateCount = otherSimpleInfoMapper.updateByPrimaryKeySelective(otherSimpleInfo);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			otherSimpleInfo.initDeleteData(otherSimpleInfo.getModifier(), otherSimpleInfo.getModifierName());
			// 影响的记录数
			int deleteCount = otherSimpleInfoMapper.updateByPrimaryKeySelective(otherSimpleInfo);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<OtherSimpleInfo> selectByExt(OtherSimpleInfoExt otherSimpleInfoExt) {
		return otherSimpleInfoMapper.selectByExt(otherSimpleInfoExt);
	}
	
	@Override
	public int countByExt(OtherSimpleInfoExt otherSimpleInfoExt){
		return otherSimpleInfoMapper.countByExt(otherSimpleInfoExt);
	}
	
	@Override
	public OtherSimpleInfo selectById(String id) {
		return otherSimpleInfoMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			otherSimpleInfoMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(OtherSimpleInfo otherSimpleInfo) {
		return otherSimpleInfoMapper.countRefined(otherSimpleInfo);
	}

	@Override
	public OtherSimpleInfo selectRefined(OtherSimpleInfo otherSimpleInfo) {
		return otherSimpleInfoMapper.selectRefined(otherSimpleInfo);
	}
	
	@Override
	public int deletedRefined(OtherSimpleInfo otherSimpleInfo) {
		return otherSimpleInfoMapper.deletedRefined(otherSimpleInfo);
	}

	@Override
	public boolean repeat(OtherSimpleInfo otherSimpleInfo) {
		int count = countRefined(otherSimpleInfo);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(otherSimpleInfo.getId())) {
			return true;
		}
		OtherSimpleInfo dbItem = selectRefined(otherSimpleInfo);
		if(null == dbItem) {
			return false;
		}
		return !otherSimpleInfo.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return otherSimpleInfoMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<OtherSimpleInfo> otherSimpleInfos) {
		return otherSimpleInfoMapper.insertBatch(otherSimpleInfos);
	}
}
